icontheme: Add gtk_icon_theme_has_gicon
authorMatthias Clasen <mclasen@redhat.com>
Fri, 26 Mar 2021 17:15:54 +0000 (13:15 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 26 Mar 2021 17:17:51 +0000 (13:17 -0400)
Add a utility function to check whether the icontheme
will produce something better than missing-image for
a GIcon. Obviously, we can only answer this question
if the GIcon is a themed icon the begin with.

gtk/gtkicontheme.c
gtk/gtkicontheme.h

index 7486457e176c64dc65ccdb0450cb3385ec91aa71..45623c44d4cf475a2e3cd643a3b735f030984da2 100644 (file)
@@ -2615,6 +2615,50 @@ gtk_icon_theme_has_icon (GtkIconTheme *self,
   return res;
 }
 
+/**
+ * gtk_icon_theme_has_gicon:
+ * @self: a `GtkIconTheme`
+ * @gicon: a `GIcon`
+ *
+ * Checks whether an icon theme includes an icon
+ * for a particular `GIcon`.
+ *
+ * Returns: %TRUE if @self includes an icon for @gicon
+ */
+gboolean
+gtk_icon_theme_has_gicon (GtkIconTheme *self,
+                          GIcon        *gicon)
+{
+  const char * const *names;
+  gboolean res = FALSE;
+
+  if (!G_IS_THEMED_ICON (gicon))
+    return TRUE;
+
+  names = g_themed_icon_get_names (G_THEMED_ICON (gicon));
+
+  gtk_icon_theme_lock (self);
+
+  ensure_valid_themes (self, FALSE);
+
+  for (int i = 0; names[i]; i++)
+    {
+      for (GList *l = self->themes; l; l = l->next)
+        {
+          if (theme_has_icon (l->data, names[i]))
+            {
+              res = TRUE;
+              goto out;
+            }
+        }
+    }
+
+ out:
+  gtk_icon_theme_unlock (self);
+
+  return res;
+}
+
 static void
 add_size (gpointer key,
           gpointer value,
index 1db2d1766f72c3d5b7724c7abb137307f2b4be75..183c7249aa8f1ac69c2418c04842cf83aea80c48 100644 (file)
@@ -116,6 +116,9 @@ char *           gtk_icon_theme_get_theme_name       (GtkIconTheme
 GDK_AVAILABLE_IN_ALL
 gboolean         gtk_icon_theme_has_icon             (GtkIconTheme                *self,
                                                       const char                  *icon_name);
+GDK_AVAILABLE_IN_4_2
+gboolean         gtk_icon_theme_has_gicon            (GtkIconTheme                *self,
+                                                      GIcon                       *gicon);
 GDK_AVAILABLE_IN_ALL
 int              *gtk_icon_theme_get_icon_sizes      (GtkIconTheme                *self,
                                                       const char                  *icon_name);